package com.dr.service.impl;

import com.dr.Enum.ExcepEnum;
import com.dr.common.ution.DateUtil;
import com.dr.db.SysConfigMapper;
import com.dr.exception.ServiceExcep;
import com.dr.pojo.SysConfig;
import com.dr.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tugou on 2017/5/11.
 */
@Service
public class SysConfigServiceImp implements SysConfigService{

    @Autowired
    SysConfigMapper sysConfigMapper;

    @Override
    public List<SysConfig> getSysConfig(SysConfig config) {
        List<SysConfig> list = sysConfigMapper.getConfigByName(config);
        return list;
    }

    @Override
    public List<SysConfig> getConfigList() {
        return sysConfigMapper.getConfigByName(null);
    }

    @Override
    public void saveConfig(SysConfig config) throws ServiceExcep {
        config.setState(1);
        config.setCreateDate(DateUtil.now());
        int result = sysConfigMapper.insertSysConfig(config);
        if (result == 0){
            throw new ServiceExcep(ExcepEnum.INSERTDATAEMPTY.getName(),ExcepEnum.INSERTDATAEMPTY.getIndex());
        }
    }

    @Override
    public void updateConfig(SysConfig config) throws ServiceExcep {
        config.setCreateDate(DateUtil.now());
        int result = sysConfigMapper.updateSysConfig(config);
        if(result == 0){
            throw new ServiceExcep(ExcepEnum.INSERTDATAEMPTY.getName(),ExcepEnum.INSERTDATAEMPTY.getIndex());
        }
    }

    @Override
    public void commitConfig(SysConfig config) throws ServiceExcep {
        if(config.getId() == null){
            this.saveConfig(config);
        }else{
            this.updateConfig(config);
        }

    }
}
